package cn.com.api.push;

import java.util.List;
import java.util.Map;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import cn.jpush.api.JPushClient;
import cn.jpush.api.common.resp.APIConnectionException;
import cn.jpush.api.common.resp.APIRequestException;
import cn.jpush.api.push.PushResult;
import cn.jpush.api.push.model.Message;
import cn.jpush.api.push.model.Options;
import cn.jpush.api.push.model.Platform;
import cn.jpush.api.push.model.PushPayload;
import cn.jpush.api.push.model.audience.Audience;
import cn.jpush.api.push.model.notification.Notification;

public class IOSToSingleDevice {

	protected static final Logger LOG = LogManager.getLogger(IOSToSingleDevice.class.getName());

	private static IOSToSingleDevice singleton;

	private IOSToSingleDevice() {
	}

	public static IOSToSingleDevice getInstance() {
		if (singleton == null) {
			singleton = new IOSToSingleDevice();
		}
		return singleton;
	}

	public void iostosingledevice(String appKey, String masterSecret, String alert, Map<String, String> customKeyVaules,
			List<String> registrationIds) throws APIConnectionException, APIRequestException {
		// 1. get apiKey and secretKey from developer console
		JPushClient jpushClient = new JPushClient(masterSecret, appKey, 3);

		// 2. build a BaidupushClient object to access released interfaces
		PushPayload payload = PushPayload.newBuilder().setPlatform(Platform.ios())
				.setAudience(Audience.registrationId(registrationIds))
				.setNotification(Notification.ios(alert, customKeyVaules))
				.setOptions(Options.newBuilder().setApnsProduction(true).build()).build();

		try {

			PushResult result = jpushClient.sendPush(payload);
			LOG.info("Got result - " + result);
		} catch (APIConnectionException e) {
			LOG.error("Connection error. Should retry later. ", e);

		} catch (APIRequestException e) {
			LOG.error("Error response from JPush server. Should review and fix it. ", e);
			LOG.info("HTTP Status: " + e.getStatus());
			LOG.info("Error Code: " + e.getErrorCode());
			LOG.info("Error Message: " + e.getErrorMessage());
			LOG.info("Msg ID: " + e.getMsgId());
		}
	}

	/** 发送消息 */
	public void iostosingleDeviceMessage(Map<String, String> params, String appKey, String masterSecret,
			List<String> registrationIds, String message) throws APIConnectionException, APIRequestException {
		JPushClient jpushClient = new JPushClient(masterSecret, appKey, 3);
		PushPayload payload = PushPayload.newBuilder().setPlatform(Platform.ios())
				.setAudience(Audience.registrationId(registrationIds))
				.setOptions(Options.newBuilder().setApnsProduction(true).build())
				.setMessage(Message.newBuilder().setMsgContent(message).addExtras(params).build()).build();

		try {
			PushResult result = jpushClient.sendPush(payload);
			LOG.info("Got result - " + result);
		} catch (APIConnectionException e) {
			LOG.error("Connection error. Should retry later. ", e);
		} catch (APIRequestException e) {
			LOG.error("Error response from JPush server. Should review and fix it. ", e);
			LOG.info("HTTP Status: " + e.getStatus());
			LOG.info("Error Code: " + e.getErrorCode());
			LOG.info("Error Message: " + e.getErrorMessage());
			LOG.info("Msg ID: " + e.getMsgId());
		}
	}

}
